//
//  LocationDao.m
//  JiaoX
//
//  Created by yinqy on 15/1/16.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "LocationDao.h"
#import "FMDatabase.h"
#import "AddressDbBean.h"

#define PROVINCE_TABLE @"provinces"
#define CITY_TABLE @"province_city"
#define AREA_TABLE @"city_area"

@interface LocationDao()
{
    FMDatabase *db;
}
@end
@implementation LocationDao
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSString *database_path = [[NSBundle mainBundle] pathForResource:@"location" ofType:@"db"];;
        db = [FMDatabase databaseWithPath:database_path];
    }
    return self;
}

/**
 * 得到省份的array
 *
 * @return
 */
-(NSArray *)getProvinces{
    NSMutableArray *array = [NSMutableArray array];
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@",PROVINCE_TABLE];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString * name = [rs stringForColumn:@"name"];
            int ID = [rs intForColumn:@"Id"];
            AddressDbBean *bean = [[AddressDbBean alloc]init];
            [bean setProvinceId:ID];
            [bean setProvinceName:name];
            [array addObject:bean];
        }
        [db close];
        return [NSArray arrayWithArray:array];
    }
    return array;
}

/**
 * 得到城市的Array
 *
 * @return
 */
-(NSArray *)getCitys:(int)provinceId{
    NSMutableArray *array = [NSMutableArray array];
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE province_id = '%d'",CITY_TABLE,provinceId];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString * name = [rs stringForColumn:@"city"];
            int ID = [rs intForColumn:@"id"];
            AddressDbBean *bean = [[AddressDbBean alloc]init];
            [bean setCityId:ID];
            [bean setCityName:name];
            [array addObject:bean];
        }
        [db close];
        return [NSArray arrayWithArray:array];
    }
    return array;
}

/**
 * 得到县的array
 *
 * @return
 */
-(NSArray *)getAreas:(int)cityId{
    NSMutableArray *array = [NSMutableArray array];
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE city_id = '%d'",AREA_TABLE,cityId];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString * name = [rs stringForColumn:@"area"];
            int ID = [rs intForColumn:@"id"];
            AddressDbBean *bean = [[AddressDbBean alloc]init];
            [bean setAreaId:ID];
            [bean setAreaName:name];
            [array addObject:bean];
        }
        [db close];
        return [NSArray arrayWithArray:array];
    }
    return array;
}

/**
 * 查询指定id的省级名称
 *
 * @param provinceId 省级id
 * @return
 */
-(NSString *)getProvinceName:(int)provinceId{
    NSString *result = @"";
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT name FROM %@ WHERE Id = '%d'",PROVINCE_TABLE,provinceId];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs stringForColumnIndex:0];
        }
        [db close];
        return result;
    }
    return result;
}

/**
 * 查询指定id的市级名称
 *
 * @param cityId 市级id
 * @return
 */
-(NSString *)getCityName:(int)cityId{
    NSString *result = @"";
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT city FROM %@ WHERE id = '%d'",CITY_TABLE,cityId];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs stringForColumnIndex:0];
        }
        [db close];
        return result;
    }
    return result;
}

/**
 * 查询指定id的县级名称
 *
 * @param areaId 县级id
 * @return
 */
-(NSString *)getAreaName:(int)areaId{
    NSString *result = @"";
    if([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT area FROM %@ WHERE id = '%d'",AREA_TABLE,areaId];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            result = [rs stringForColumnIndex:0];
        }
        [db close];
        return result;
    }
    return result;
}

/**
 * 获取省市县组合的名字
 *
 * @param provinceId
 * @param cityId
 * @param areaId
 * @param spliteStr  分割线
 * @return
 */
-(NSString *)getDetaild:(int)provinceId :(int)cityId :(int)areaId :(NSString *)spliteStr{
    NSMutableString *result = [NSMutableString string];
    [result appendString:[self getProvinceName:provinceId]];
    [result appendString:spliteStr];
    [result appendString:[self getCityName:cityId]];
    [result appendString:spliteStr];
    [result appendString:[self getAreaName:areaId]];
    return result;
}

@end
